﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

using InfFront.InfoViews.Base;

namespace InfFront.InfoViews.WPF
{
    /// <summary>
    /// Interaction logic for MenuTabsFrame.xaml
    /// </summary>
    public partial class MenuTabsFrame : UserControl
    {
        #region Private Variables
        private RootInfoViewSource rootSource = null;
        #endregion

        #region Construction/Initialization
        public MenuTabsFrame()
        {
            Init();
        }

        public MenuTabsFrame(RootInfoViewSource source)
        {
            rootSource = source;
            DataContext = source;

            Init();

            Rebuild();
        }

        private void Init()
        {
            InitializeComponent();
        }

        public void SetSource(RootInfoViewSource source)
        {
            rootSource = source;
            DataContext = source;

            Rebuild();
        }
        #endregion

        private void Rebuild()
        {
            if (rootSource.IsDummy)
                mnuSources.ItemsSource = rootSource.NestedSources;
            else
                mnuSources.ItemsSource = new System.Collections.ArrayList() { rootSource };

            // restore the previously selected tab
            var settings = rootSource.VisualSettings;
            var srcPath = (string[])settings.GetValue(Settings.Manager.LAST_SELECTED_SOURCE_KEY);
            var selectedView = rootSource.FindByPath(srcPath);
            if (selectedView != null)
            {
                tabs.SelectedIndex = tabs.Items.Add(selectedView);
            }
        }

        private void menuItem_Click(object sender, RoutedEventArgs e)
        {
            var mi = (MenuItem)sender;
            var src = (InfoViewSource)mi.DataContext;

            if (src.IsDummy)
            {
                e.Handled = true;
                return;
            }

            int tabIndex = tabs.Items.IndexOf(src);
            if (tabIndex == -1)
                tabIndex = tabs.Items.Add(src);

            tabs.SelectedIndex = tabIndex;

            var settings = rootSource.VisualSettings;
            settings.SetValue(Settings.Manager.LAST_SELECTED_SOURCE_KEY, src.Path);

            e.Handled = true;
        }

        private void menuItem_MouseEnter(object sender, MouseEventArgs e)
        {
            var mi = (MenuItem)sender;
            if (mi.HasItems)
                mi.IsSubmenuOpen = true;

        }

        private void menuItem_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            var mi = (MenuItem)sender;
            if (mi.HasItems && ((FrameworkElement)e.OriginalSource).DataContext == mi.DataContext)
            {
                mi.IsSubmenuOpen = false;
                menuItem_Click(sender, e);
            }
            e.Handled = true;

        }

        private void tabCloseButton_Click(object sender, RoutedEventArgs e)
        {
            var btn = (FrameworkElement)sender;

            tabs.Items.Remove(btn.DataContext);
            e.Handled = true;
        }
    }
}
